package org.jeecg.modules.hc.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.hc.entity.HcMerchant;
import org.jeecg.modules.hc.enums.HcSyncMerchantSettlementTypeEnum;
import org.jeecg.modules.hc.service.IHcMerchantService;
import org.jeecg.modules.hc.service.IHcMerchantSettlementService;
import org.jeecg.modules.hc.service.IHcOrderMerchantService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.List;

/**
 * 订单、返现和门店状态处理计划任务
 *
 * @Author Scott
 */
@Slf4j
public class HcSyncMerchantSettlementJob implements Job {

    @Autowired
    private IHcMerchantSettlementService hcMerchantSettlementService;

    @Autowired
    private IHcMerchantService hcMerchantService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        long start = System.currentTimeMillis();
        log.info("同步商家结算计划任务开始：" + jobExecutionContext.getJobDetail().getKey());
        List<HcMerchant> hcMerchantList = hcMerchantService.list();

        for (HcMerchant hcMerchant : hcMerchantList) {
            try {
                hcMerchantSettlementService.syncMerchantSettlement(hcMerchant.getId(), HcSyncMerchantSettlementTypeEnum.YESTERDAY);
            } catch (Exception e) {
                log.error("同步商家结算计划任务报错，商户id：{}", hcMerchant.getId());
                e.printStackTrace();
            }
        }

        log.info("同步商家结算计划任务结束，耗时 " + (System.currentTimeMillis() - start) + " 毫秒");
    }
}
